function split_by_pipe(str,pref)
    local result = {}
    local start = 1
    local pos
    if string.sub(str, 1, 2) == pref then
        table.insert(result,"")
    end
    -- 循环查找'|'字符  
    while true do
        pos = string.find(str, pref, start)
        if not pos then
            -- 没有找到'|'，将剩余部分添加到结果中  
            break
        end
        -- 将'|'之前的部分添加到结果中  
        table.insert(result, string.sub(str, start, pos - 1))
        -- 更新起始位置，跳过'|'  
        start = pos + 1
    end
    -- 将最后一个部分（可能不存在'|'）添加到结果中  
    if start <= string.len(str) then
        table.insert(result, string.sub(str, start))
    end
    if string.sub(str, string.len(str)) == pref then
        table.insert(result,"")
    end

    return result
end


Scenes = {}

function Scenes:new(result)
    local obj = {
        result = result,
        scenes = nil      -- 应用场景
    }
    setmetatable(obj, self)
    self.__index = self
    obj:parseScene()
    return obj
end

function Scenes:getScenes()
    return self.scenes
end

function Scenes:parseScene()
    local infos = {}
    for value in string.gmatch(self.result, "[^|]+") do
        table.insert(infos, value)
    end
    if #infos ~= 1 then error("查询结果格式错误或离线库非场景库") end
    local scene_map = {
        COM = "商业公司",
        EDU = "教育机构",
        GTW = "企业专线",
        GOV = "政府机构",
        NET = "基础设施",
        ORG = "组织机构",
        DYN = "家庭宽带",
        IDC = "数据中心",
        MOB = "移动网络",
        CDN = "内容分发",
        DNS = "域名解析",
        BOGON = "保留"
    }

    -- 获取结果
    local scene = scene_map[infos[1]] or "-"  -- 如果找不到匹配，返回 "-"
end

Location = {}

function Location:new(result)
    local obj = {
        result = result,
        continent = nil,       -- 洲
        country = nil,         -- 国家/地区
        province = nil,        -- 省份
        city = nil,            -- 城市
        district = nil,        -- 区县
        isp = nil,             -- 运营商
        areaCode = nil,        -- 区域代码
        countryEnglish = nil,  -- 国家/地区英文
        countryCode = nil,     -- 国家/地区英文简写
        longitude = nil,       -- 经度
        latitude = nil,        -- 纬度
        elevation = nil,       -- 海拔
        zipCode = nil,         -- 邮编
        cityCode = nil,        -- 城市代码
        weatherStation = nil,  -- 气象站
        asn = nil,             -- ASN
        preserve = nil,        -- 预留
        street = nil,          -- 街道
        timeZone = nil         -- 时区
    }
    setmetatable(obj, self)
    self.__index = self
    obj:parseLocation()
    return obj
end

function Location:getContinent()
    return self.continent
end

function Location:getCountry()
    return self.country
end

function Location:getProvince()
    return self.province
end

function Location:getCity()
    return self.city
end

function Location:getDistrict()
    return self.district
end

function Location:getIsp()
    return self.isp
end

function Location:getAreaCode()
    return self.areaCode
end

function Location:getCountryEnglish()
    return self.countryEnglish
end

function Location:getCountryCode()
    return self.countryCode
end

function Location:getLongitude()
    return self.longitude
end

function Location:getLatitude()
    return self.latitude
end

function Location:getElevation()
    return self.elevation
end

function Location:getZipCode()
    return self.zipCode
end

function Location:getCityCode()
    return self.cityCode
end

function Location:getWeatherStation()
    return self.weatherStation
end

function Location:getAsn()
    return self.asn
end

function Location:getPreserve()
    return self.preserve
end

function Location:getStreet()
    return self.street
end

function Location:getTimeZone()
    return self.timeZone
end


function Location:parseLocation()
    local infos = split_by_pipe(self.result,"|")

    if #infos ~= 19 then error("查询结果格式错误或离线库非归属地库") end
    self.continent = infos[1]
    self.country = infos[2]
    self.province = infos[3]
    self.city = infos[4]
    self.district = infos[5]
    self.isp = infos[6]
    self.areaCode = infos[7]
    self.countryEnglish = infos[8]
    self.countryCode = infos[9]
    self.longitude = infos[10]
    self.latitude = infos[11]
    self.elevation = infos[12]
    self.zipCode = infos[13]
    self.cityCode = infos[14]
    self.weatherStation = infos[15]
    self.asn = infos[16]
    self.preserve = infos[17]
    self.street = infos[18]
    self.timeZone = infos[19]
end

function Location:toString()
    return string.format("Location{continent='%s', country='%s', province='%s', city='%s', district='%s', isp='%s', areaCode='%s', countryEnglish='%s', countryCode='%s', longitude='%s', latitude='%s', elevation='%s', zipCode='%s', cityCode='%s', weatherStation='%s', asn='%s', preserve='%s', street='%s', timeZone='%s'}",
        self.continent, self.country, self.province, self.city, self.district, self.isp, self.areaCode, self.countryEnglish, self.countryCode, self.longitude, self.latitude, self.elevation, self.zipCode, self.cityCode, self.weatherStation, self.asn, self.preserve, self.street, self.timeZone)
end



-- 代理v2版本
Proxy = {}

-- 构造函数
function Proxy:new(result)
    local obj = {}
    setmetatable(obj, self)
    self.__index = self
    self.result = result
    self.isProxy = ""
    self.proxyType = ""
    self.proxyTime = ""
    obj:parseProxy()
    return obj
end

-- 解析代理信息
function Proxy:parseProxy()
    local infos = split_by_pipe(self.result,"|")

    if #infos ~= 3 then
        print("查询结果格式错误或离线库非代理v2版本库")
        self.proxyTime = ""
        self.proxyType = ""
        return
    end
    self.isProxy = infos[1]
    local proxyTypes = {}
    for match in (infos[2] .. ","):gmatch("(.-),") do
        if match ~= "" then
            table.insert(proxyTypes, match)
        end
    end

    self.proxyType = table.concat(proxyTypes, "|")
    self.proxyTime = infos[3]
end

-- 获取代理类型
function Proxy:getProxyType()
    return self.proxyType
end

-- 获取代理时间
function Proxy:getProxyTime()
    return self.proxyTime
end

function Proxy:getIsProxy()
    return self.isProxy
end



-- Example usage
return {
    Scenes = Scenes,
    Location = Location,
    Proxy = Proxy,
}